<?php

namespace app\admin\controller;

use app\admin\service\CommonService;
use app\models\AnswerAnalysisModel;
use app\models\ArrangingModel;
use app\models\CampusModel;
use app\models\ClassRoomModel;
use app\models\CommentModel;
use app\models\ContractModel;
use app\models\CourseContractModel;
use app\models\CourseLogModel;
use app\models\CourseModel;
use app\models\GoodsContractModel;
use app\models\GradesContractModel;
use app\models\GradesDetailsListsModel;
use app\models\GradesDetailsModel;
use app\models\StudentModel;
use app\models\StudentSubjectClassModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\UserModel;
use app\models\WorkModel;
use think\Session;
use think\session\driver\Redis;

/**
 * Class Arranging
 * @package app\admin\controller
 */
class Arranging extends Common
{

    /**
     * 物品与合同管理模型.
     *
     *  @var GoodsContractModel
     */
    protected $goodsContractModel;

    /**
     * 班级与合同管理模型.
     *
     *  @var GradesContractModel
     */
    protected $gradesContractModel;

    /**
     * 报读记录.
     *
     * @var CourseLogModel
     */
    protected $courseLogModel;


    /**
     * 校区管理模型.
     *
     * @var CampusModel
     */
    protected $campusModel;

    /**
     * 班级管理详情模型.
     *
     * @var GradesDetailsModel
     */
    protected $gradesdetailModel;

    /**
     * 学生分班管理模型.
     *
     * @var StudentSubjectClassModel
     */
    protected $studentsubjectclassModel;

    /**
     * 学生分班排课管理模型.
     *
     * @var StudentSubjectInfosClassModel
     */
    protected $studentsubjectinfosclassModel;

    /**
     * 学生信息模型.
     *
     * @var StudentModel
     */
    protected $studentModel;

    /**
     * 班级管理列表详情模型.
     *
     * @var GradesDetailsListsModel
     */
    protected $gradesdetaillistModel;

    /**
     * 排课模型.
     *
     * @var ArrangingModel
     */
    protected $arrangingModel;

    /**
     * 课程模型.
     *
     * @var CourseModel
     */
    protected $courseModel;
    /**
     * 教室管理模型.
     *
     * @var ClassRoomModel
     */
    protected $classroomModel;

    /**
     * 课程订单关系模型.
     *
     * @var CourseContractModel
     */
    protected $courseContractModel;

    /**
     * 订单管理模型.
     *
     * @var ContractModel
     */
    protected $contractModel;

    /**
     * 排课管理.
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $course = $this->gradesdetaillistModel->getArrangingLists($param);
			$where = [];
			if(get_current_type() == 4 || get_current_type() == 5){
				$where['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){
				$where['campus_id'] = ['in',get_current_campus_id()];
			}
			//查询未绑定学员的订单id
			$ids = $this->contractModel->where($where)->where(['student_id'=>null])->column('id');
			if(!empty($ids)){
				//删除未绑定学员的订单数据
				$this->contractModel->where($where)->where(['student_id'=>null])->delete();
				//删除关联表课程订单数据
				$ids = implode(',',$ids);
				$this->courseContractModel->where($where)->where(['contract_id'=>['in',$ids]])->delete();
                $this->gradesContractModel->where($where)->where(['contract_id'=>['in',$ids]])->delete();
                $this->goodsContractModel->where($where)->where(['contract_id'=>['in',$ids]])->delete();
			}
            session("contract_id",null);
            $this->success('success', '', $course, 0);
        }
		$where = [];
		if(get_current_type() == 4 || get_current_type() == 5){
			$where['campus_id'] = get_current_campus_id();
		}elseif (get_current_type() == 2 || get_current_type() == 3){
			$where['campus_id'] = ['in',get_current_campus_id()];
		}
        $course_list = $this->courseModel->where($where)->select(); //课程名称
        $campus = $this->campusModel->where($where)->select()->toArray();
        $grades_result = $this->gradesdetailModel->where($where)->where(['graduation_status'=>0])->select(); //未结业
        return $this->fetch('',['grade' => $grades_result,'course_list' => $course_list,'campus' => $campus]);
    }

	/**
	 * 课节详情
	 * modify by Small Y
	 *
	 * @return mixed
	 * @throws \think\Exception
	 * @throws \think\exception\DbException
	 */
    public function arrang_course()
	{
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $param['class_manage_id'] = input('grades_id');
            $course = $this->gradesdetaillistModel->getArrangingLists($param);
            $where = [];
			if(get_current_type() == 4 || get_current_type() == 5){
				$where['campus_id'] = get_current_campus_id();
			}elseif (get_current_type() == 2 || get_current_type() == 3){
				$where['campus_id'] = ['in',get_current_campus_id()];
			}
            //查询未绑定学员的订单id
            $ids = $this->contractModel->where($where)->where(['student_id'=>null])->column('id');
            if(!empty($ids)){
                //删除未绑定学员的订单数据
                $this->contractModel->where($where)->where(['student_id'=>null])->delete();
                //删除关联表课程订单数据
                $ids = implode(',',$ids);
				$this->courseContractModel->where($where)->where(['contract_id'=>['in',$ids]])->delete();
                $this->gradesContractModel->where($where)->where(['contract_id'=>['in',$ids]])->delete();
                $this->goodsContractModel->where($where)->where(['contract_id'=>['in',$ids]])->delete();
            }
            session("contract_id",null);
            $this->success('success', '', $course, 0);
        }
        $grades_id = input('grades_id'); //班级ID
        $grades_status = input('grades_status'); //来源标识
        $campus_id = input('campus_id');
        $grades_result = $this->gradesdetailModel->get(['id' => $grades_id]);
        $grades_result['course_name'] = $this->courseModel->get(['id' => $grades_result['course_id']])['course_name'];

        if($grades_result['graduation_status'] == 0){
            if($grades_result['status'] == 1){
                $grades_result['grades_status'] = '已开班';
            }else if($grades_result['status'] == 0){
                $grades_result['grades_status'] = '未开班';
            }
        }else if($grades_result['graduation_status'] == 1){
            $grades_result['grades_status'] = '已结业';
        }
		$where = [];
		$where['campus_id'] = $campus_id;
        $grades_result['already_arranged'] = $this->gradesdetaillistModel->where($where)->where(['status'=>1,'class_manage_id' => $grades_id])->count();
        $grades_result['campus_name'] = $this->campusModel->get(['id' => $grades_result['campus_id']])['campus_name'];
        $grades_result['seat_num'] = $this->studentsubjectclassModel->where($where)->where(['seat_number'=>['neq',null],'class_subject_id'=>$grades_id])->count();
        $grades_result['enrollment_status'] = $this->courseLogModel->where(['course' => ['like', $grades_result['course_id'].'%']])->count();
        if($grades_result['preoccupation'] == 0){
            $grades_result['seat_num'] = $grades_result['seat_num'].'/'.'不限';
        }else{
            $grades_result['seat_num'] = $grades_result['seat_num'].'/'.$grades_result['preoccupation'];
        }
        $room_id = explode(',',$grades_result['room_id']);
        $room_id = array_unique($room_id);
        $room_res= $this->classroomModel->where(['id' => array('in',implode(',',$room_id))])->column('class_name');
        $grades_result['room_text'] = implode(',',$room_res);
        return $this->fetch('',[
        	'grades_status' => $grades_status ,
			'grades_result' => $grades_result,
			'campus_id' => $campus_id,
			'grades_id' => $grades_id
		]);

    }

    /**
     * 添加排课.
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $data['campus_id'] = get_current_campus_id();
            $data['cost_type'] = 89;
            $data['price'] = $data['price'] * 100;
            $res = $this->arrangingModel->save($data);
            $course['id'] = $this->arrangingModel->id;
            $course['course_name'] = $this->arrangingModel->course_name;
            if (!$res) {
                $this->error('添加排课失败', null, '', 1);
            }
            $this->success('添加成功', '', $course, 1);
        }
        $this->dictionary();
        return $this->fetch('',['year'=>date('Y')]);
    }

    /**
     * 改变状态
     *
     * @return \think\response\Json
     */
    public function arranging_check_status()
    {
        if (request()->isPost()) {
            try {
                $data = input('post.');
                if ($data['status'] == 0) {
                    $msg = '禁用';
                } else {
                    $msg = '启用';
                }
                CourseModel::update($data);
                return json(['code' => 0, 'msg' => $msg]);
            } catch (\Exception $e) {
                return json(['code' => 1, 'msg' => $e->getMessage()]);
            }
        }
    }

    /**
        点名上课
     **/

    public function roll_call(){
        $json = '';
        $class_status = 0;
        $content_area = '';
        $remarks_area = '';
        if(isset($_GET["id"]) && isset($_GET["campus_id"])){
            $id =  input('id'); //节课id
            $campus_id =  input('campus_id'); //节课id
            $class_manage_id = $this->gradesdetaillistModel->get(['id' => $id])['class_manage_id'];
            $class_status = $this->gradesdetailModel->get(['id' => $class_manage_id])['class_status'];
            $key_json = 'jsonstudent';
            $key_status = 'status';
            $json_session =  session($key_json);
            $content_area = session('content_area'); //课节内容
            $remarks_area = session('remarks_area'); //课节备注
            $json = json_decode($json_session);
            if(count($json) > 0){
                $search_name = '';
                if(session($key_status) == 1){
                    $search_name = session('find_name');

                }

                return $this->fetch('',[
                	'class_status' => $class_status ,
					'jsondata' =>$json_session,
					'id' => $id,
					'campus_id' => input('campus_id'),
					'search_name' => $search_name,
					'content_area' =>$content_area,
					'remarks_area' => $remarks_area
				]);
            }
            $result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->select();
			$where = [];
			$where['campus_id'] = $campus_id;
            $student_result = $this->studentModel->where($where)->where(['status'=>1])->select();
            $where['class_subject_id'] = $class_manage_id; //班级ID
            $student_result_list = $this->studentsubjectclassModel->where($where)->select();
            $student_data = [];
            foreach ($student_result as $key => $value){
                $student_data[$value['id'].''] = $value['phone'];
            }

            $student_result_list_data = [];
            foreach ($student_result_list as $key => $value){
                    $item = [];
                    $item['class_subject_id'] = $value['class_subject_id'];
                    $item['student_type'] = $value['student_type'];
                    $item['room_id'] = $value['room_id'];
                    $item['student_id'] = $value['student_id'];
                    $item['status'] = $value['status'];
                    $item['contract_number'] = $value['contract_number'];
                    $student_result_list_data[] = $item;
            }


            $data_json = [];   //返回前台json
            foreach ($result as $key => $value){
                    $item = [];
                    $student_type = 1;
                    for($a = 0; $a < count($student_result_list_data); $a ++){
                        if($student_result_list_data[$a]['contract_number'] ==  $value['contract_number'] && $student_result_list_data[$a]['status'] ==  $value['report_status'] && $student_result_list_data[$a]['room_id'] == $value['room_id'] && $student_result_list_data[$a]['class_subject_id'] ==  $value['class_subject_id'] && $student_result_list_data[$a]['student_id'] ==  $value['student_id']){
                            $student_type = $student_result_list_data[$a]['student_type']; //学员类型
                        }
                    }

                    if ($student_type == 1) {
                        $item['student_name'] = $value['student_name'];//非补课学员
                    } else if ($student_type == 2) {
                        $item['student_name'] = $value['student_name'] . '_【临加】';//临加学员
                    } else if ($student_type == 3) {
                        $item['student_name'] = $value['student_name'] . '_【补课】';//补课学员
                    }else if ($student_type == 4) {
                        $item['student_name'] = $value['student_name'] . '_【试听】';//试听学员
                    }

                    $item['student_type'] = $student_type; //学生类型
                    $item['class_manage_details_id'] = $value['class_manage_details_id'];
                    $item['seat_number'] = $value['seat_number'];
                    $item['class_subject_id'] = $value['class_subject_id'];
                    $item['room_id'] = $value['room_id'];
                    $item['student_id'] = $value['student_id'];
                    $item['surplus_class_elimination'] = $value['surplus_class_elimination'].'(赠送'.$value['surplus_class_elimination_give'].')'; //剩余课时
                    $item['surplus_class_elimination_give'] = $value['surplus_class_elimination_give']; //赠送剩余课时
                    $item['id'] = $value['id'];
                    $item['phone'] = $student_data[$value['student_id'].''];
                    $data_json[] = $item;

            }
            $json = json_encode($data_json);
            session($key_json,$json);
            session($key_status,-1);

        }

        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $id = $data['id'];//节课id
			$campus_id = $data['campus_id'];
            $key_json = 'jsonstudent';
            $key_status = 'status';
            $student_name = $data['student_name'];//搜索名称
            $student_lists = $data['student_lists'];//学生列表
            $content_area = $data['content_area']; //课节内容
            $remarks_area = $data['remarks_area']; //课节备注
            $student_lists = json_decode($student_lists, true);
            $data_json = [];   //返回前台json
            for($i = 0 ; $i < count($student_lists); $i ++){
                $cell = $student_lists[$i];
                $item = [];
                if(strpos( $cell['student_name'],$student_name) !== false){
                    $item['student_name'] = $cell['student_name'];
                    $item['class_manage_details_id'] = $cell['class_manage_details_id'];
                    $item['seat_number'] = $cell['seat_number'];
                    $item['class_subject_id'] = $cell['class_subject_id'];
                    $item['room_id'] = $cell['room_id'];
                    $item['student_id'] = $cell['student_id'];
                    $item['surplus_class_elimination'] = $cell['surplus_class_elimination']; //剩余课时
                    $item['surplus_class_elimination_give'] = $cell['surplus_class_elimination_give']; //赠送剩余课时
                    $item['id'] = $cell['id'];
                    $item['student_type'] = $cell['student_type'];
                    $item['phone'] = $cell['phone'];
                    $data_json[] = $item;
                }

            }

            if(count($data_json) <= 0){
                $this->error('未搜索到该学生', null, -1, 1);
            }
            $json = json_encode($data_json);
            session($key_json,$json);
            session($key_status,1);
            session('find_name',$student_name);
            session('content_area',$content_area);  //课节内容
            session('remarks_area',$remarks_area); //课节备注
            $this->success('完成搜索', '', '', 1);
        }

        session('find_name',null);
        return $this->fetch('',[
        	'class_status' => $class_status ,
			'jsondata' => $json,
			'id' => input('id'),
			'search_name' => '',
			'content_area' => $content_area,
			'campus_id' => input('campus_id'),
			'remarks_area' => $remarks_area
		]);
    }


    /***删除单个学生课节**/
    public function remove_item(){

        //未上课状态才能执行此操作
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $content_area = $data['content_area']; //课节内容
            $remarks_area = $data['remarks_area']; //课节备注
            $id = $data['id'];

            $result = $this->studentsubjectinfosclassModel->get(['id' => $id]);
            $class_manage_details_id = $result['class_manage_details_id'];
            $class_subject_id = $result['class_subject_id'];
            if($result['lessons_id'] == -1){  //不是补课记录
                if(!$this->studentsubjectinfosclassModel->where(['id' => $id])->delete()){
                    $this->error('删除该学生课节失败', null, -1, 1);
                }
            }else{

                if($result['follow_status'] == 1) {  //新开班课节

                    $id_ = $result['lessons_id'];
                    if(!$this->studentsubjectinfosclassModel->save(['lessons_status' => 0],['id' => $id_])){
                        $this->error('更新该学生状态失败', null, -1, 1);
                    }

                    if(!$this->studentsubjectinfosclassModel->where(['id' => $id])->delete()){
                        $this->error('删除该学生课节失败', null, -1, 1);
                    }

                    if($this->studentsubjectinfosclassModel->where(['class_subject_id' => $class_subject_id])->count() <= 0){ //是否删完

                        if(!$this->studentsubjectclassModel->where(['class_subject_id' => $class_subject_id])->delete()){
                            $this->error('删除分班补课学生失败', null, -1, 1);
                        }

                        if($this->gradesdetaillistModel->where(['id' => $class_manage_details_id])->count() > 0) {
                            if (!$this->gradesdetaillistModel->where(['id' => $class_manage_details_id])->delete()) {
                                $this->error('删除该补课学生课节失败', null, -1, 1);
                            }
                        }
                        if($this->gradesdetailModel->where(['id' => $class_subject_id])->count() > 0){
                            if(!$this->gradesdetailModel->where(['id' => $class_subject_id])->delete()){
                                $this->error('删除该补课学生班级失败', null, -1, 1);
                            }

                        }


                    }
                }else if($result['follow_status'] == 2) {  //新开班课节

                    $id_ = $result['lessons_id'];
                    if(!$this->studentsubjectinfosclassModel->save(['lessons_status' => 0],['id' => $id_])){
                        $this->error('更新该学生状态失败', null, -1, 1);
                    }

                    if(!$this->studentsubjectinfosclassModel->where(['id' => $id])->delete()){
                        $this->error('删除该学生课节失败', null, -1, 1);
                    }


                    if(!$this->studentsubjectclassModel->where(['class_subject_id' => $class_subject_id, 'student_type' => 3, 'student_id' => $result['student_id']])->delete()){
                        $this->error('删除分班补课学生失败', null, -1, 1);
                    }

                }

            }

            /**更新session学生信息**/
            $key_json = 'jsonstudent';
            $json_date = session($key_json);
            $json_date = json_decode($json_date,true);
            for($j = 0; $j < count($json_date); $j ++){
                if($json_date[$j]['id'] == $id){
                    unset($json_date[$j]);
                    break;
                }
            }
            $json = array_values($json_date);
            $json = json_encode($json);

            session($key_json,$json);
            session('content_area',$content_area);  //课节内容
            session('remarks_area',$remarks_area); //课节备注

            $this->success('删除成功', '', '', 1);

        }

    }

    /***删除所有学生课节**/
    public function remove_all(){
        //未上课状态才能执行此操作
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $content_area = $data['content_area']; //课节内容
            $remarks_area = $data['remarks_area']; //课节备注
            $id = $data['id'];


            $result = $this->gradesdetaillistModel->get(['id' => $id]);
            $result_grade_status = $this->gradesdetailModel->get(['id' => $result['class_manage_id']])['class_status'];
            if($result_grade_status == 0){
                $result_info_result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id'=> $id])->select(); //获取当前所有该节课学生课时
                foreach ($result_info_result as $key => $value){
                    if($value['follow_status'] == 2){   //跟班学员

                        $id_ = $value['lessons_id'];
                        if(!$this->studentsubjectinfosclassModel->save(['lessons_status' => 0],['id' => $id_])){
                            $this->error('更新该学生状态失败', null, -1, 1);
                        }

                        if(!$this->studentsubjectinfosclassModel->where(['id' => $value['id']])->delete()){
                            $this->error('删除该学生课节失败', null, -1, 1);
                        }


                        if(!$this->studentsubjectclassModel->where(['class_subject_id' => $value['class_subject_id'], 'student_type' => 3, 'student_id' => $value['student_id']])->delete()){
                            $this->error('删除分班补课学生失败', null, -1, 1);
                        }


                    }else{
                        $this->studentsubjectinfosclassModel->where(['id' => $value['id']])->delete();
                    }
                }

            }else{

                //释放补课学员
                $student_lesson_result =  $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->select();


                foreach ($student_lesson_result as $key => $value){
                    $lessons_id = $value['lessons_id']; //所绑课程ID
                    if(!$this->studentsubjectinfosclassModel->where(['id' => $lessons_id])->update(['lessons_status' => 0])){
                        $this->error('更新该补课学生状态失败', null, -1, 1);
                    }
                }

                if(!$this->studentsubjectclassModel->where(['class_subject_id' => $result['class_manage_id']])->delete()){
                    $this->error('删除分班补课学生失败', null, -1, 1);
                }

                if(!$this->gradesdetaillistModel->where(['id' => $id])->delete()){
                    $this->error('删除该补课学生课节失败', null, -1, 1);
                }
                if(!$this->gradesdetailModel->where(['id' => $result['class_manage_id']])->delete()){
                    $this->error('删除该补课学生班级失败', null, -1, 1);
                }

                if(!$this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->delete()){

                    $this->error('删除补课所有学生课节失败', null, -1, 1);
                }

            }


            /**更新session学生信息**/
            $key_json = 'jsonstudent';
            session($key_json,null);
            session('content_area',$content_area);  //课节内容
            session('remarks_area',$remarks_area); //课节备注

            $this->success('删除成功', '', '', 1);

        }

    }


    /****
     *
     * 获取上课内容
     */

    public function get_content(){
        $content_area = '';
        $remarks_area = '';
        $id = '';
        $campus_id = '';
        if(isset($_GET["id"])) {
            $id = $_GET["id"];//节课ID
			$campus_id = $_GET["campus_id"];
            $result = $this->studentsubjectinfosclassModel->get(['class_manage_details_id' => $id]);
            $content_area = $result['content_area']; //课节内容
            $remarks_area = $result['remarks_area']; //备注

        }
        return $this->fetch('change_content',['id' => $id,'campus_id' => $campus_id, 'content_area' => $content_area, 'remarks_area' => $remarks_area]);
    }

    /***修改节课内容**/
    public function submit_Content(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $content_area = $data['content_area']; //课节内容
            $remarks_area = $data['remarks_area']; //备注
            $id = $data['id']; //节课ID
            if(!$this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->update(['content_area' => $content_area, 'remarks_area' => $remarks_area])){
                $this->error('节课内容未更改', null, -1, 1);
            }
            $this->success('更新节课内容成功', '', '', 1);


        }

    }


    /**
        重置
     */
    public function rest(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $id = $data['id'];//节课id
            $key_json = 'jsonstudent';
            $key_status = 'status';
            $content_area = $data['content_area']; //课节内容
            $remarks_area = $data['remarks_area']; //课节备注
            session('content_area',$content_area);  //课节内容
            session('remarks_area',$remarks_area); //课节备注
            $result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->select();
			$where = [];
			$where['campus_id'] = $data['campus_id'];
			$where['status'] = 1;
            $student_result = $this->studentModel->where($where)->select();
            $student_data = [];
            foreach ($student_result as $key => $value){
                $student_data[$value['id'].''] = $value['phone'];
            }

            $data_json = [];   //返回前台json
            foreach ($result as $key => $value){
                    $item = [];
                    $student_type = $this->studentsubjectclassModel->get(['status' => $value['report_status'], 'contract_number' => $value['contract_number'], 'class_subject_id' => $value['class_subject_id'], 'room_id' =>$value['room_id'] , 'student_id' => $value['student_id']])['student_type'];
                    if ($student_type == 1) {
                        $item['student_name'] = $value['student_name'];//非补课学员
                    } else if ($student_type == 2) {
                        $item['student_name'] = $value['student_name'] . '_【临加】';//临加学员
                    } else  if ($student_type == 3) {
                        $item['student_name'] = $value['student_name'] . '_【补课】';//补课学员
                    } else if ($student_type == 4) {
                        $item['student_name'] = $value['student_name'] . '_【试听】';//试听学员
                    }

                    $item['class_manage_details_id'] = $value['class_manage_details_id'];
                    $item['seat_number'] = $value['seat_number'];
                    $item['class_subject_id'] = $value['class_subject_id'];
                    $item['room_id'] = $value['room_id'];
                    $item['student_id'] = $value['student_id'];
                    $item['id'] = $value['id'];
                    $item['phone'] = $student_data[$value['student_id'].''];
                    $item['surplus_class_elimination'] = $value['surplus_class_elimination']; //剩余课时
                    $item['surplus_class_elimination_give'] = $value['surplus_class_elimination_give']; //赠送剩余课时
                    $data_json[] = $item;
            }
            if(count($data_json) <= 0){
                $this->error('未分配学生', null, -1, 1);
            }
            $json = json_encode($data_json);
            session($key_json,$json);
            session($key_status,-1);
            session('find_name',null);
            $this->success('重置成功', '', '', 1);
        }

    }


    /**
    关闭
     */
    public function close(){
        /*
       $redis->connect('47.106.196.41','6379');
       $redis->auth('cdY8hlZulZ'); //密码,无密码则无需填充
       $redis->set($key,$value);
       $redis->get($key);
       $redis->close();
        */

        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $id = $data['id'];//节课id
            $key_json = 'jsonstudent';
            $key_status = 'status';
            session($key_json,null);
            session($key_status,-1);
            session('find_name',null);
            session('content_area',null);  //课节内容
            session('remarks_area',null); //课节备注
            $this->success('已关闭', '', '', 1);
        }

    }

    /**
        *提交infos课节
     **/

    public function submission(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $arr_json = $data['arr_json'];
            $id = $data['id']; //节课id
            $result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->select();
            $class_subject_id = -1;  //该班级ID
            $infos = [];
            $public_course_id = -1;  //公共课程ID
            foreach ($result as $key => $value){
                        $item = [];
                        $class_subject_id = $value['class_subject_id'];
                        $item['contract_number'] = $value['contract_number'];
                        $item['id'] = $value['id'];
                        $item['course_id'] = $value['course_id'];
                        $item['surplus_class_elimination'] = $value['surplus_class_elimination'];
                        $item['surplus_class_elimination_give'] = $value['surplus_class_elimination_give'];
                        $item['charging'] =  $value['charging'];
                        $item['report_status'] = $value['report_status']; // 来源
                        $public_course_id = $value['course_id'];
                        $postion_key = $item['id'].'';
                        $infos[$postion_key] = $item;
            }


            $update_save_data = [];
            $update_save_contract_data = [];
            $update_arrears_status_student_arr = [];   //欠费状态数组
            $arr_json_lists = json_decode($arr_json, true);
            for ($i = 0; $i < count($arr_json_lists); $i++) {
                $item = $arr_json_lists[$i];
                $cell = [];

                $cell['attendance'] = $item['attendance'];  //出勤
                $cell['charging'] = $item['charging'];  //计费

                $cell['id'] = $item['id'];  //id
                $cell['student_id'] = $item['student_id'];  //student_id
                $cell['attendance_reasons'] = $item['attendance_reasons'];  //缺勤reasons
                $cell['remarks'] = $item['remarks'];  //备注
                $cell['remarks_area'] = $item['remarks_area'];  //课节备注
                $cell['content_area'] = $item['content_area'];  //课节内容

                $contract_number = $infos[$item['id'].'']['contract_number'];  //对应的订单号
                $course_id = $infos[$item['id'].'']['course_id'];  //对应的课程ID
                $res =  $this->contractModel->get(['contract_number' => $contract_number]);  //订单表
                $id_ = $res['id']; //订单ID

                $price_ = 0;  //单价
                $course_elimination_arr = []; //课消
                $course_elimination_arr_give = []; //赠送课消
                $pos = -1;
                $payment_balance = 0; //剩余余额
                if($infos[$item['id'].'']['report_status'] == 1){  //课程
                    $res_course = $this->courseContractModel->get(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']]);
                    $payment_balance = $res_course['payment_balance'];
                    $course_ = $res['course'];  //课程
                    $course_elimination_ = $res['course_elimination']; //课消
                    $course_elimination_give_ = $res['give_course_nums']; //赠送课消
                    $course_arr = explode(',',$course_);
                    $course_elimination_arr = explode(',',$course_elimination_);
                    $course_elimination_arr_give = explode(',',$course_elimination_give_);
                    for ($j = 0; $j < count($course_arr); $j ++){
                        if($course_arr[$j] == $course_id){
                            $pos = $j;
                            break;
                        }
                    }
                    if($res_course['class_num'] == 0){
						//算单价
						$price_ = 0;
					}else{
						//算单价
						$price_ = round(((($res_course['net_receipts'] - $res_course['assessed_amount']))/($res_course['class_num'])),2); //折后应收 - 分摊金额
					}
                }else if($infos[$item['id'].'']['report_status'] == 2){ //班级
                    $res_grades = $this->gradesContractModel->get(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']]);
                    $payment_balance = $res_grades['payment_balance'];
                    $course_grades_ = $res['grades_course'];  //课程
                    $course_grades_elimination_ = $res['grades_course_elimination']; //课消
                    $course_grades_elimination_give_ = $res['give_grades_course_nums']; //赠送课消
                    $course_grades_arr = explode(',',$course_grades_);
                    $course_elimination_arr = explode(',',$course_grades_elimination_);
                    $course_elimination_arr_give = explode(',',$course_grades_elimination_give_);
                    for ($j = 0; $j < count($course_grades_arr); $j ++){
                        if($course_grades_arr[$j] == $course_id){
                            $pos = $j;
                            break;
                        }
                    }
					if($res_grades['class_num'] == 0){
						//算单价
						$price_ = 0;
					}else{
						//算单价
						$price_ = round(((($res_grades['net_receipts'] - $res_grades['assessed_amount']))/($res_grades['class_num'])),2); //折后应收 - 分摊金额
					}
                }else if($infos[$item['id'].'']['report_status'] == 3){ //试听

                }

                if($item['charging'] == 1) {  //如果计费
                    $cell['amount_deducted'] = $price_ * $item['eliminated_nums'];  //扣除金额
                    $cell['eliminated_nums'] = $item['eliminated_nums'];
                }else{
                    $cell['amount_deducted'] = 0;
                    $cell['eliminated_nums'] = 0;
                }


                if($pos >= 0 && $course_elimination_arr_give[$pos] > 0 && $course_elimination_arr[$pos] <= 0){
                    $cell['surplus_class_elimination_give'] = $infos[$item['id'].'']['surplus_class_elimination_give'] - $cell['eliminated_nums']; //赠送剩余课消
                    $cell['surplus_class_elimination'] = $infos[$item['id'].'']['surplus_class_elimination'];
                    $cell['give_status'] = 2;  //赠送课程
                    $cell['amount_deducted'] = 0;  //赠送不计课消金额
                }else{  //先扣非赠送
                    $cell['surplus_class_elimination'] = $infos[$item['id'].'']['surplus_class_elimination'] - $cell['eliminated_nums']; //剩余课消取值实际购买课消数
                    $cell['surplus_class_elimination_give'] = $infos[$item['id'].'']['surplus_class_elimination_give'];
                    $cell['give_status'] = 1;  //非赠送课程
                }



                $cell['status'] = 1; //1表示已上课
                //操作人
                $cell['operatorer'] = get_current_name();  //操作人
                $cell['operatorer_id'] = get_current_id();  //操作人ID


                //更新订单表数据
                $contract_cell = [];
                $course_elimination_arr[$pos] = $cell['surplus_class_elimination'];//剩余课消
                $course_elimination_arr_give[$pos] = $cell['surplus_class_elimination_give'];//赠送剩余课消

                $course_elimination_str = implode(',',$course_elimination_arr); //更新后的课消
                $course_elimination_give_str = implode(',',$course_elimination_arr_give); //赠送更新后的课消

                $course_balance = 0;
                $expenditure = 0;
                if($cell['give_status'] = 1) {
                    $expenditure = $res['expenditure']; //支出
                    $expenditure = ($expenditure + $cell['amount_deducted']); //更新支出金额
                    //更新单项缴费余额
                    if($infos[$item['id'].'']['report_status'] == 1) {  //课程
                        $payment_balance = $payment_balance - $cell['amount_deducted'];
                        $this->courseContractModel->where(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']])->update(['payment_balance' => $payment_balance]);

                        if($payment_balance < 0){
                            $cell['arrears_amount'] =  $payment_balance;  //欠费金额
                            $cell['arrears_status'] = 1;//欠费
                        }
                    }else if($infos[$item['id'].'']['report_status'] == 2) {  //班级
                        $payment_balance = $payment_balance - $cell['amount_deducted'];
                        $this->gradesContractModel->where(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']])->update(['payment_balance' => $payment_balance]);

                        if($payment_balance < 0){
                            $cell['arrears_amount'] =  $payment_balance;  //欠费金额
                            $cell['arrears_status'] = 1;//欠费
                        }
                    }else if($infos[$item['id'].'']['report_status'] == 3){  //试听

                    }
                    $update_save_data[] = $cell;  //详细课节信息


                    //学费余额
                    $payment_grades_balance = $this->gradesContractModel->where(['contract_id' => $res['id']])->sum('payment_balance');
                    $payment_course_balance = $this->courseContractModel->where(['contract_id' => $res['id']])->sum('payment_balance');
                    $payment_money_balance = ($payment_course_balance + $payment_grades_balance); //班级与课程余额之和
                    $course_balance = $payment_money_balance; //更新后的账户余额
                }else if($cell['give_status'] = 2){
                    $expenditure = $res['expenditure'];
                    $course_balance = $res['balance'];
                }
                if($infos[$item['id'].'']['report_status'] == 1) {  //课程
                    $contract_cell['course_elimination'] = $course_elimination_str;  //課消剩余
                    $contract_cell['give_course_nums'] = $course_elimination_give_str;  //赠送课消剩余
                }else if($infos[$item['id'].'']['report_status'] == 2){  //班级
                    $contract_cell['grades_course_elimination'] = $course_elimination_str;  //課消剩余
                    $contract_cell['give_grades_course_nums'] = $course_elimination_give_str;  //赠送课消剩余
                }else if($infos[$item['id'].'']['report_status'] == 3){  //试听

                }
                $contract_cell['balance'] = $course_balance; //学费剩余金额
                $contract_cell['expenditure'] = $expenditure; //支出
                $contract_cell['id'] = $id_; //订单ID
                $update_save_contract_data[] = $contract_cell;


                //更新剩余课节详细表
                $this->studentsubjectinfosclassModel->where(['student_id' => $item['student_id'],'class_subject_id' => $class_subject_id])->update(['surplus_class_elimination' => $cell['surplus_class_elimination']]);
                //更新赠送剩余课节详细表
                $this->studentsubjectinfosclassModel->where(['student_id' => $item['student_id'],'class_subject_id' => $class_subject_id])->update(['surplus_class_elimination_give' => $cell['surplus_class_elimination_give']]);


                //更新学员表欠费状态
                if($contract_number != null && $contract_number != '---'){
                    $item_arrears = [];
                    $item_arrears['id'] = $item['student_id'];
                    if($course_balance >= 0){
                        $item_arrears['arrears_status'] = 1;  //1表示未欠费
                    }else if($course_balance < 0){
                        $item_arrears['arrears_status'] = 0;  //0表示欠费
                    }

                    $update_arrears_status_student_arr[] = $item_arrears;
                }

            }

            if(count($update_arrears_status_student_arr) >0){
                if(!$this->studentModel->saveAll($update_arrears_status_student_arr)){
                    $this->error('更新欠费信息失败', null, -1, 1);
                }
            }


            if(count($update_save_contract_data) >0){
                if(!$this->contractModel->saveAll($update_save_contract_data)){
                    $this->error('同步订单信息失败', null, -1, 1);
                }
            }

            if(count($update_save_data) > 0){
                if(!$this->studentsubjectinfosclassModel->saveAll($update_save_data)){
                    $this->error('上课点名失败', null, -1, 1);
                }
            }



            if(!$this->gradesdetaillistModel->save(['status' => 1],['id' => $id])){
                $this->error('更新上课状态失败', null, -1, 1);
            }


            $this->success('上课点名成功', '', '', 1);
        }

    }

    /***
    撤销上课
     **/
    public function cancel_grades(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $id = input('id'); //节课ID
            $result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->select();
            $result_list_id = $this->gradesdetaillistModel->get(['id' => $id])['class_manage_id']; //班级ID
            $class_subject_id = $result_list_id;

            $infos = [];
            $contract_update = [];
            $update_arrears_status_student_arr = [];
            foreach ($result as $key => $value){
                if($value['lessons_status'] == 1) {  //存在补课记录
                    $res = $this->studentsubjectinfosclassModel->get(['lessons_id' => $value['id']]);
                    $res_gradeslist = $this->gradesdetaillistModel->get(['id' => $res['class_manage_details_id']]);
                    if ($res_gradeslist['status'] == 1) {  //已上课
                        $this->success('已安排补课，需先撤销补课记录，才能撤销本次课节', '', '', 1);
                    }

                }
                //edu_class_subject_student_infos表
                $item = [];
                $item['id'] = $value['id'];   //详情ID
                $item['attendance_reasons'] = null;   //缺勤原因
                $item['remarks'] = null;//备注
                $item['remarks_area'] = null; //课节备注
                $item['content_area'] = null; //课节内容
                $item['attendance'] = 0; //考勤
                $item['charging'] = 0;  //计费
                $item['eliminated_nums'] = 0; //课消节数
                if($value['give_status'] == 1){  //非赠送
                    $item['surplus_class_elimination'] = $value['surplus_class_elimination'] + $value['eliminated_nums']; // 该课程剩余课时，默认为-1
                    $item['surplus_class_elimination_give'] = $value['surplus_class_elimination_give'];
                }else  if($value['give_status'] == 2){  //赠送
                    $item['surplus_class_elimination_give'] = $value['surplus_class_elimination_give'] + $value['eliminated_nums']; // 该课程赠送剩余课时，默认为-1
                    $item['surplus_class_elimination'] =  $value['surplus_class_elimination'];
                }
                $item['amount_deducted'] = 0; // 扣除金额
                $item['arrears_status'] = 0; // 欠费标志
                $item['arrears_amount'] = 0; // 欠费金额
                $item['status'] = 0; //表示未上课
                $item['lessons_status'] = 0;
                $item['operatorer'] = get_current_name();  //操作人
                $item['operatorer_id'] = get_current_id(); //操作人ID

                //edu_contract订单表
                $contract_number = $value['contract_number']; //对应订单编号
                $contract_result = $this->contractModel->get(['contract_number' => $contract_number]); //获取对应订单编号的数据
                $contract_item = [];
                $contract_item['contract_number'] = $contract_number;//订单编号

                if($value['give_status'] == 1) {  //非赠送
                    $expenditure = $contract_result['expenditure']; //支出
                    $expenditure = ($expenditure - $value['amount_deducted']); //更新支出金额

                    if($value['report_status'] == 1) {  //课程
                        $res_course = $this->courseContractModel->get(['student_id' =>$value['student_id'], 'course_id' => $value['course_id'],'contract_id' =>$contract_result['id'],'campus_id' => $value['campus_id']]);
                        $res_course['payment_balance'] =  $res_course['payment_balance'] + $value['amount_deducted'];
                        $this->courseContractModel->where(['id' => $res_course['id']])->update(['payment_balance' =>  $res_course['payment_balance']]);


                    }else if($value['report_status'] == 2){ //班级
                        $res_grades = $this->gradesContractModel->get(['student_id' =>$value['student_id'], 'course_id' => $value['course_id'],'contract_id' =>$contract_result['id'],'campus_id' => $value['campus_id']]);
                        $res_grades['payment_balance'] =  $res_grades['payment_balance'] + $value['amount_deducted'];
                        $this->gradesContractModel->where(['id' => $res_grades['id']])->update(['payment_balance' =>  $res_grades['payment_balance']]);

                    }else if($value['report_status'] == 3){ //试听

                    }
                    //学费余额
                    $payment_grades_balance = $this->gradesContractModel->where(['contract_id' => $contract_result['id']])->sum('payment_balance');
                    $payment_course_balance = $this->courseContractModel->where(['contract_id' => $contract_result['id']])->sum('payment_balance');
                    $payment_money_balance = ($payment_course_balance + $payment_grades_balance); //班级与课程余额之和

                    $contract_item['balance'] = $payment_money_balance; //订单余额更新
                    $contract_item['expenditure'] = $expenditure; //支出金额
                }

                $pos = -1;
                if($value['report_status'] == 1){  //课程
                    $course_arr = explode(',',$contract_result['course']);
                    for ($j = 0; $j < count($course_arr); $j ++){
                        if($course_arr[$j] == $value['course_id']){
                            $pos = $j;
                            break;
                        }
                    }
                    if($value['give_status'] == 1){  //非赠送
                        $course_elimination_arr = explode(',',$contract_result['course_elimination']);
                        $course_elimination_arr[$pos] = $item['surplus_class_elimination'];//剩余课消
                        $contract_item['course_elimination'] = implode(',',$course_elimination_arr);
                    }else if($value['give_status'] == 2){  //赠送
                        $course_elimination_arr_give = explode(',',$contract_result['give_course_nums']);
                        $course_elimination_arr_give[$pos] = $item['surplus_class_elimination_give'];//赠送剩余课消
                        $contract_item['give_course_nums'] =  implode(',',$course_elimination_arr_give);
                    }

                }else if($value['report_status'] == 2){  //班级
                    $grades_course_arr = explode(',',$contract_result['grades_course']);
                    for ($j = 0; $j < count($grades_course_arr); $j ++){
                        if($grades_course_arr[$j] == $value['course_id']){
                            $pos = $j;
                            break;
                        }
                    }
                    if($value['give_status'] == 1){  //非赠送
                        $course_elimination_arr = explode(',',$contract_result['grades_course_elimination']);
                        $course_elimination_arr[$pos] = $item['surplus_class_elimination'];//剩余课消
                        $contract_item['grades_course_elimination'] = implode(',',$course_elimination_arr);
                    }else if($value['give_status'] == 2){  //赠送
                        $course_elimination_arr_give = explode(',',$contract_result['give_grades_course_nums']);
                        $course_elimination_arr_give[$pos] = $item['surplus_class_elimination_give'];//赠送剩余课消
                        $contract_item['give_grades_course_nums'] = implode(',',$course_elimination_arr_give);
                    }

                }else if($value['report_status'] == 3){ //试听

                }
                $contract_item['id'] = $contract_result['id'];
                $contract_update[] = $contract_item;
                $infos[] = $item;

                //更新课节表剩余课节数量
                $this->studentsubjectinfosclassModel->where(['student_id' => $value['student_id'],'class_subject_id' => $class_subject_id])->update(['surplus_class_elimination' => $item['surplus_class_elimination']]);
                //更新课节表剩余课节数量
                $this->studentsubjectinfosclassModel->where(['student_id' => $value['student_id'],'class_subject_id' => $class_subject_id])->update(['surplus_class_elimination_give' => $item['surplus_class_elimination_give']]);


                //更新学员表欠费状态
                if($contract_number != null && $contract_number != '---'){
                    $item_arrears = [];
                    $item_arrears['id'] = $value['student_id'];
                    if($contract_item['balance'] >= 0){
                        $item_arrears['arrears_status'] = 1;  //1表示未欠费
                    }else if($contract_item['balance'] < 0){
                        $item_arrears['arrears_status'] = 0;  //0表示欠费
                    }
                    $update_arrears_status_student_arr[] = $item_arrears;
                }


             }

            if(count($update_arrears_status_student_arr) >0){
                if(!$this->studentModel->saveAll($update_arrears_status_student_arr)){
                    $this->error('更新学生欠费状态失败', null, -1, 1);
                }
            }


             /*******删除未开课的补课记录********/
            foreach ($result as $key => $value) {
                if ($value['lessons_status'] == 1) {  //存在补课记录
                    $res = $this->studentsubjectinfosclassModel->get(['lessons_id' => $value['id']]);
                        if($res){
                            $res_gradeslist = $this->gradesdetaillistModel->get(['id' => $res['class_manage_details_id']]);
                            if($res_gradeslist){
                                if ($res_gradeslist['status'] == 0) {  //未上课

                                    if($res['follow_status'] == 1){  //新开班课节

                                        $this->gradesdetaillistModel->where(['id' => $res['class_manage_details_id']])->delete();//删除节课列表
                                        $this->gradesdetailModel->where(['id' => $res['class_subject_id']])->delete();
                                        $this->studentsubjectclassModel->where(['class_subject_id' => $res['class_subject_id']])->delete();
                                        $this->studentsubjectinfosclassModel->where(['class_subject_id' => $res['class_subject_id']])->delete();


                                    }else if($res['follow_status'] == 2){  //跟班课节


                                        $this->studentsubjectclassModel->where(['class_subject_id' => $res['class_subject_id'], 'student_type' => 3, 'student_id' => $res['student_id']])->delete();
                                        $this->studentsubjectinfosclassModel->where(['class_subject_id' => $res['class_subject_id'],'follow_status' => 2, 'student_id' => $res['student_id']])->delete();

                                    }

                                }
                            }

                        }

                }
            }
            /***************END*******************/


             //edu_class_subject_student_infos表更新上课状态
            if(count($infos) > 0){
                if(!$this->studentsubjectinfosclassModel->saveAll($infos)){
                    $this->error('更新当前课节失败', null, -1, 1);
                }
            }

            //edu_contract表更新上课状态
            if(count($contract_update) > 0){
                if(!$this->contractModel->saveAll($contract_update)){
                    $this->error('更新订单信息失败', null, -1, 1);
                }

            }

            //edu_class_manage_details_lists表更新上课状态
            if(!$this->gradesdetaillistModel->save(['status' => 0],['id' => $id])){
                $this->error('请勿重复更新状态', null, -1, 1);
            }

            $this->success('撤销成功', '', '', 1);
        }
    }


    /****
     *
     * 删除课节**
     */

    public function class_del(){

        if ($this->request->isAjax() && $this->request->isPost()) {

            $result = $this->gradesdetaillistModel->get(['id' => input('id')]);
            $result_class_status = $this->gradesdetailModel->get(['id' => $result['class_manage_id']])['class_status'];

            if($result_class_status == 0){
                if (!$this->gradesdetaillistModel->where(['id' => input('id')])->delete()) {
                    $this->error('删除课节失败', null, '', 1);
                }

                if($this->studentsubjectinfosclassModel->where(['class_manage_details_id' => input('id')])->count() > 0){
                    if (!$this->studentsubjectinfosclassModel->where(['class_manage_details_id' => input('id')])->delete()) {
                        $this->error('删除详情课节失败', null, '', 1);
                    }
                }

            }else{
               //补课班级
                $student_lesson_result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => input('id')])->select();

                foreach ($student_lesson_result as $key => $value){
                    $lessons_id = $value['lessons_id']; //所绑课程ID
                    if(!$this->studentsubjectinfosclassModel->where(['id' => $lessons_id])->update(['lessons_status' => 0])){
                        $this->error('更新该补课学生状态失败', null, -1, 1);
                    }
                }

                if(!$this->studentsubjectclassModel->where(['class_subject_id' => $result['class_manage_id']])->delete()){
                    $this->error('删除分班补课学生失败', null, -1, 1);
                }

                if(!$this->gradesdetaillistModel->where(['id' => input('id')])->delete()){
                    $this->error('删除该补课学生课节失败', null, -1, 1);
                }
                if(!$this->gradesdetailModel->where(['id' => $result['class_manage_id']])->delete()){
                    $this->error('删除该补课学生班级失败', null, -1, 1);
                }

                if(!$this->studentsubjectinfosclassModel->where(['class_manage_details_id' => input('id')])->delete()){

                    $this->error('删除补课所有学生课节失败', null, -1, 1);
                }

            }


            $this->success('删除课节成功', url('Teacher/lists'), '', 1);
        }
    }






    /***删除单个学生课节**/
    public function modify_remove_item(){

        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $content_area = $data['content_area']; //课节内容
            $remarks_area = $data['remarks_area']; //课节备注
            $id = $data['id'];

            if(!$this->studentsubjectinfosclassModel->where(['id' => $id])->delete()){
                $this->error('删除该学生课节失败', null, -1, 1);
            }

            session('content_area',$content_area);  //课节内容
            session('remarks_area',$remarks_area); //课节备注

            $this->success('删除成功', '', '', 1);

        }

    }



    /***删除所有学生课节**/
    public function modify_remove_all(){

        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $content_area = $data['content_area']; //课节内容
            $remarks_area = $data['remarks_area']; //课节备注
            $ids = $data['ids'];
            $ids_arr = explode(',',$ids);

            for($i = 0 ;$i < count($ids_arr); $i ++){
                $id = $ids_arr[$i];
                if(!$this->studentsubjectinfosclassModel->where(['id' => $id])->delete()){
                    $this->error('删除该学生课节失败', null, -1, 1);
                }

            }

            session('content_area',$content_area);  //课节内容
            session('remarks_area',$remarks_area); //课节备注

            $this->success('删除成功', '', '', 1);

        }

    }

    /************提交学员修改信息***************/
    public function modify_submission(){

        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $arr_json = $data['arr_json'];
            $id = $data['id']; //节课id
            $result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->select();
            $class_subject_id = -1;
            $infos = [];
            $public_course_id = -1;  //公共课程ID
            foreach ($result as $key => $value){

                $class_subject_id = $value['class_subject_id'];  //班级ID
                /**************撤销单个课节******************/
                $contract_result_now = $this->contractModel->get(['contract_number' => $value['contract_number']]); //获取对应订单编号的数据
                $old_eliminated_nums = $value['eliminated_nums'];  //课消数量
                $old_surplus_class_elimination = $value['surplus_class_elimination']; //剩余课时
                $old_surplus_class_elimination_give = $value['surplus_class_elimination_give']; //赠送剩余课时

                $pos_now = -1;
                if($value['report_status'] == 1){  //课程
                    $course_arr_now = explode(',',$contract_result_now['course']);
                    for ($j = 0; $j < count($course_arr_now); $j ++){
                        if($course_arr_now[$j] == $value['course_id']){
                            $pos_now = $j;
                            break;
                        }
                    }
                }else if($value['report_status'] == 2){  //班级
                    $course_arr_now_grades = explode(',',$contract_result_now['grades_course']);
                    for ($j = 0; $j < count($course_arr_now_grades); $j ++){
                        if($course_arr_now_grades[$j] == $value['course_id']){
                            $pos_now = $j;
                            break;
                        }
                    }
                }else if($value['report_status'] == 3){  //试听

                }


                //更新订单表数据
                $contract_cell_update = [];
                if($value['report_status'] == 1){  //课程
                    if($value['give_status'] == 1){
                        $course_elimination_now = $contract_result_now['course_elimination']; //课消
                        $course_elimination_now_arr = explode(',',$course_elimination_now);
                        $course_elimination_now_arr[$pos_now] = ($old_surplus_class_elimination + $old_eliminated_nums);//剩余课消
                        $course_elimination_str_now = implode(',',$course_elimination_now_arr);//更新后的课消
                        $contract_cell_update['course_elimination'] = $course_elimination_str_now;  //课消

                        $res_course = $this->courseContractModel->get(['student_id' =>$value['student_id'], 'course_id' => $value['course_id'],'contract_id' =>$contract_result_now['id'],'campus_id' => $value['campus_id']]);
                        $res_course['payment_balance'] = $res_course['payment_balance']  + $value['amount_deducted'];
                        $this->courseContractModel->where(['id' => $res_course['id']])->update(['payment_balance' =>  $res_course['payment_balance']]);


                    }else if($value['give_status'] == 2){
                        $course_elimination_now = $contract_result_now['give_course_nums']; //赠送课消
                        $course_elimination_now_arr = explode(',',$course_elimination_now);
                        $course_elimination_now_arr[$pos_now] = ($old_surplus_class_elimination_give + $old_eliminated_nums);//赠送剩余课消
                        $course_elimination_str_now = implode(',',$course_elimination_now_arr);//更新后的课消
                        $contract_cell_update['give_course_nums'] = $course_elimination_str_now;  //课消
                    }
                }else if($value['report_status'] == 2){  //班级
                    if($value['give_status'] == 1){
                        $course_elimination_now = $contract_result_now['grades_course_elimination']; //课消
                        $course_elimination_now_arr = explode(',',$course_elimination_now);
                        $course_elimination_now_arr[$pos_now] = ($old_surplus_class_elimination + $old_eliminated_nums);//剩余课消
                        $course_elimination_str_now = implode(',',$course_elimination_now_arr);//更新后的课消
                        $contract_cell_update['grades_course_elimination'] = $course_elimination_str_now;  //课消

                        $res_grades = $this->gradesContractModel->get(['student_id' =>$value['student_id'], 'course_id' => $value['course_id'],'contract_id' =>$contract_result_now['id'],'campus_id' => $value['campus_id']]);
                        $res_grades['payment_balance'] = $res_grades['payment_balance']  + $value['amount_deducted'];
                        $this->gradesContractModel->where(['id' => $res_grades['id']])->update(['payment_balance' =>  $res_grades['payment_balance']]);


                    }else if($value['give_status'] == 2){
                        $course_elimination_now = $contract_result_now['give_grades_course_nums']; //赠送课消
                        $course_elimination_now_arr = explode(',',$course_elimination_now);
                        $course_elimination_now_arr[$pos_now] = ($old_surplus_class_elimination_give + $old_eliminated_nums);//赠送剩余课消
                        $course_elimination_str_now = implode(',',$course_elimination_now_arr);//更新后的课消
                        $contract_cell_update['give_grades_course_nums'] = $course_elimination_str_now;  //课消
                    }

                }else if($value['report_status'] == 3){  //试听

                }

                $expenditure = $contract_result_now['expenditure']; //支出
                $expenditure  =  ($expenditure - $value['amount_deducted']); //更新支出金额

                //学费余额
                $payment_grades_balance = $this->gradesContractModel->where(['contract_id' => $contract_result_now['id']])->sum('payment_balance');
                $payment_course_balance = $this->courseContractModel->where(['contract_id' => $contract_result_now['id']])->sum('payment_balance');
                $payment_money_balance = ($payment_course_balance + $payment_grades_balance); //班级与课程余额之和


                $course_balance = $payment_money_balance; //更新后的账户学费余额

                $balance_now = $course_balance; //更新余额
                $contract_id = $contract_result_now['id']; //订单ID

                $contract_cell_update['balance'] = $balance_now;  //余额
                $contract_cell_update['expenditure'] = $expenditure; //支出金额

                if(count($contract_cell_update) > 0){
                    $this->contractModel->where(['id' => $contract_id])->update($contract_cell_update);
                }
                /*****************END************************/
                $item = [];
                $item['contract_number'] = $value['contract_number'];
                $item['id'] = $value['id'];
                $item['course_id'] = $value['course_id'];
                if($value['give_status'] == 1) {
                    $item['surplus_class_elimination'] = ($old_surplus_class_elimination + $old_eliminated_nums);
                    $item['surplus_class_elimination_give'] = $value['surplus_class_elimination_give'];
                }
                if($value['give_status'] == 2) {
                    $item['surplus_class_elimination_give'] = ($old_surplus_class_elimination_give + $old_eliminated_nums);
                    $item['surplus_class_elimination'] = $value['surplus_class_elimination'];
                }
                $item['charging'] =  $value['charging'];
                $item['report_status'] =  $value['report_status'];
                $public_course_id = $value['course_id'];
                $postion_key = $item['id'].'';
                $infos[$postion_key] = $item;
            }


            $update_save_data = [];
            $update_save_contract_data = [];
            $update_arrears_status_student_arr = [];  //欠费数组
            $arr_json_lists = json_decode($arr_json, true);
            for ($i = 0; $i < count($arr_json_lists); $i++) {
                $item = $arr_json_lists[$i];
                $cell = [];
                $cell['attendance'] = $item['attendance'];  //出勤
                $cell['charging'] = $item['charging'];  //计费

                $cell['id'] = $item['id'];  //id
                $cell['student_id'] = $item['student_id'];  //student_id
                $cell['attendance_reasons'] = $item['attendance_reasons'];  //缺勤reasons
                $cell['remarks'] = $item['remarks'];  //备注
                $cell['remarks_area'] = $item['remarks_area'];  //课节备注
                $cell['content_area'] = $item['content_area'];  //课节内容

                $contract_number = $infos[$item['id'].'']['contract_number'];  //对应的订单号
                $course_id = $infos[$item['id'].'']['course_id'];  //对应的课程ID
                $res =  $this->contractModel->get(['contract_number' => $contract_number]);  //订单表
                $id_ = $res['id']; //订单ID

                $price_ = 0;  //单价
                $course_elimination_arr = []; //课消
                $course_elimination_arr_give = []; //赠送课消
                $pos = -1;
                $payment_balance = 0; //剩余余额
                if($infos[$item['id'].'']['report_status'] == 1){  //课程
                    $res_course = $this->courseContractModel->get(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']]);
                    $payment_balance = $res_course['payment_balance'];
                    $course_ = $res['course'];  //课程
                    $course_elimination_ = $res['course_elimination']; //课消
                    $course_elimination_give_ = $res['give_course_nums']; //赠送课消
                    $course_arr = explode(',',$course_);
                    $course_elimination_arr = explode(',',$course_elimination_);
                    $course_elimination_arr_give = explode(',',$course_elimination_give_);
                    for ($j = 0; $j < count($course_arr); $j ++){
                        if($course_arr[$j] == $course_id){
                            $pos = $j;
                            break;
                        }
                    }
                    //算单价
                    $price_ = round(((($res_course['net_receipts'] - $res_course['assessed_amount']))/($res_course['class_num'])),2); //折后应收 - 分摊金额

                }else if($infos[$item['id'].'']['report_status'] == 2){ //班级
                    $res_grades = $this->gradesContractModel->get(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']]);
                    $payment_balance = $res_grades['payment_balance'];
                    $course_grades_ = $res['grades_course'];  //课程
                    $course_grades_elimination_ = $res['grades_course_elimination']; //课消
                    $course_grades_elimination_give_ = $res['give_grades_course_nums']; //赠送课消
                    $course_grades_arr = explode(',',$course_grades_);
                    $course_elimination_arr = explode(',',$course_grades_elimination_);
                    $course_elimination_arr_give = explode(',',$course_grades_elimination_give_);
                    for ($j = 0; $j < count($course_grades_arr); $j ++){
                        if($course_grades_arr[$j] == $course_id){
                            $pos = $j;
                            break;
                        }
                    }
                    //算单价
                    $price_ = round(((($res_grades['net_receipts'] - $res_grades['assessed_amount']))/($res_grades['class_num'])),2); //折后应收 - 分摊金额

                }else if($infos[$item['id'].'']['report_status'] == 3){ //试听

                }



                if($item['charging'] == 1) {  //如果计费
                    $cell['amount_deducted'] = $price_ * $item['eliminated_nums'];  //扣除金额
                    $cell['eliminated_nums'] = $item['eliminated_nums'];
                }else{
                    $cell['amount_deducted'] = 0;
                    $cell['eliminated_nums'] = 0;
                }

                if($pos >= 0 && $course_elimination_arr_give[$pos] > 0 && $course_elimination_arr[$pos] <= 0){  //先扣非赠送
                    $cell['surplus_class_elimination_give'] = $infos[$item['id'].'']['surplus_class_elimination_give'] - $cell['eliminated_nums']; //赠送剩余课消
                    $cell['surplus_class_elimination'] = $infos[$item['id'].'']['surplus_class_elimination'];
                    $cell['give_status'] = 2;  //赠送课程
                    $cell['amount_deducted'] = 0;  //赠送不计课消金额
                }else{
                    $cell['surplus_class_elimination'] = $infos[$item['id'].'']['surplus_class_elimination'] - $cell['eliminated_nums']; //剩余课消
                    $cell['surplus_class_elimination_give'] = $infos[$item['id'].'']['surplus_class_elimination_give'];
                    $cell['give_status'] = 1;  //非赠送课程
                }
                $cell['status'] = 1; //1表示已上课

                //操作人
                $cell['operatorer'] = get_current_name();  //操作人
                $cell['operatorer_id'] = get_current_id(); //操作人ID

                //更新订单表数据
                $contract_cell = [];
                $course_elimination_arr[$pos] = $cell['surplus_class_elimination'];//剩余课消
                $course_elimination_arr_give[$pos] = $cell['surplus_class_elimination_give'];//赠送剩余课消

                $course_elimination_str = implode(',',$course_elimination_arr); //更新后的课消
                $course_elimination_give_str = implode(',',$course_elimination_arr_give); //赠送更新后的课消

                $course_balance = 0;
                $expenditure = 0;
                if($cell['give_status'] = 1) {
                    $expenditure = $res['expenditure']; //支出
                    $expenditure = ($expenditure + $cell['amount_deducted']); //更新支出金额
                    //更新单项缴费余额
                    if($infos[$item['id'].'']['report_status'] == 1) {  //课程
                        $payment_balance = $payment_balance - $cell['amount_deducted'];
                        $this->courseContractModel->where(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']])->update(['payment_balance' => $payment_balance]);

                        if($payment_balance < 0){
                            $cell['arrears_amount'] =  $payment_balance;  //欠费金额
                            $cell['arrears_status'] = 1;//欠费
                        }else{
                            $cell['arrears_amount'] =  0;  //未欠费金额
                            $cell['arrears_status'] = 0;//未欠费
                        }
                    }else if($infos[$item['id'].'']['report_status'] == 2) {  //班级
                        $payment_balance = $payment_balance - $cell['amount_deducted'];
                        $this->gradesContractModel->where(['student_id' =>$res['student_id'], 'course_id' => $public_course_id,'contract_id' =>$res['id'],'campus_id' => $res['campus_id']])->update(['payment_balance' => $payment_balance]);

                        if($payment_balance < 0){
                            $cell['arrears_amount'] =  $payment_balance;  //欠费金额
                            $cell['arrears_status'] = 1;//欠费
                        }else{
                            $cell['arrears_amount'] =  0;  //未欠费金额
                            $cell['arrears_status'] = 0;//未欠费
                        }
                    }else if($infos[$item['id'].'']['report_status'] == 3){  //试听

                    }
                    $update_save_data[] = $cell;  //详细课节信息


                    //学费余额
                    $payment_grades_balance = $this->gradesContractModel->where(['contract_id' => $res['id']])->sum('payment_balance');
                    $payment_course_balance = $this->courseContractModel->where(['contract_id' => $res['id']])->sum('payment_balance');
                    $payment_money_balance = ($payment_course_balance + $payment_grades_balance); //班级与课程余额之和
                    $course_balance = $payment_money_balance; //更新后的账户余额


                }else if($cell['give_status'] = 2){
                    $expenditure = $res['expenditure'];
                    $course_balance = $res['balance'];
                }
                if($infos[$item['id'].'']['report_status'] == 1) {  //课程
                    $contract_cell['course_elimination'] = $course_elimination_str;  //課消剩余
                    $contract_cell['give_course_nums'] = $course_elimination_give_str;  //赠送课消剩余
                }else if($infos[$item['id'].'']['report_status'] == 2){  //班级
                    $contract_cell['grades_course_elimination'] = $course_elimination_str;  //課消剩余
                    $contract_cell['give_grades_course_nums'] = $course_elimination_give_str;  //赠送课消剩余
                }else if($infos[$item['id'].'']['report_status'] == 3){  //试听

                }

                $contract_cell['balance'] = $course_balance; //剩余金额
                $contract_cell['expenditure'] = $expenditure; //支出金额
                $contract_cell['id'] = $id_; //订单ID
                $update_save_contract_data[] = $contract_cell;

                //更新课节表剩余课节数量
                $this->studentsubjectinfosclassModel->where(['student_id' => $item['student_id'],'class_subject_id' => $class_subject_id])->update(['surplus_class_elimination' => $cell['surplus_class_elimination']]);
                //更新课节表赠送剩余课节数量
                $this->studentsubjectinfosclassModel->where(['student_id' => $item['student_id'],'class_subject_id' => $class_subject_id])->update(['surplus_class_elimination_give' => $cell['surplus_class_elimination_give']]);

                //更新学员表欠费状态
                if($contract_number != null && $contract_number != '---'){
                    $item_arrears = [];
                    $item_arrears['id'] = $item['student_id'];
                    if($course_balance >= 0){
                        $item_arrears['arrears_status'] = 1;  //1表示未欠费
                    }else if($course_balance < 0){
                        $item_arrears['arrears_status'] = 0;  //0表示欠费
                    }

                    $update_arrears_status_student_arr[] = $item_arrears;
                }


            }

            if(count($update_arrears_status_student_arr) >0){
                if(!$this->studentModel->saveAll($update_arrears_status_student_arr)){
                    $this->error('更新欠费信息失败', null, -1, 1);
                }
            }

            if(count($update_save_contract_data) >0){
                if(!$this->contractModel->saveAll($update_save_contract_data)){
                    $this->error('同步订单信息失败', null, -1, 1);
                }
            }

            if(count($update_save_data) > 0){
                if(!$this->studentsubjectinfosclassModel->saveAll($update_save_data)){
                    $this->error('更改信息失败', null, -1, 1);
                }
            }

            $this->success('更改信息成功', '', '', 1);
        }


    }

    /**********移除或者添加学员**********/
    public function modify_student(){
        $json = '';
        $class_status = 0;
        $id = '';
        $content_area = '';
        $remarks_area = '';
        $elimination_status = 0;  //动态课消默认为关闭,值为0
        if(isset($_GET["id"])){
            $id =  input('id'); //节课id
            $class_manage_id = $this->gradesdetaillistModel->get(['id' => $id])['class_manage_id'];
            $class_status = $this->gradesdetailModel->get(['id' => $class_manage_id])['class_status'];
            $result = $this->studentsubjectinfosclassModel->where(['class_manage_details_id' => $id])->select();
			$where = [];
			$where['campus_id'] = input('campus_id');
            $student_result = $this->studentModel->where($where)->where(['status'=>1])->select();
            $student_result_list = $this->studentsubjectclassModel->where($where)->select();
            $student_data = [];
            foreach ($student_result as $key => $value){
                $student_data[$value['id'].''] = $value['phone'];
            }
            $student_result_list_data = [];
            foreach ($student_result_list as $key => $value){
                $item = [];
                $item['class_subject_id'] = $value['class_subject_id'];
                $item['student_type'] = $value['student_type'];
                $item['room_id'] = $value['room_id'];
                $item['student_id'] = $value['student_id'];
                $student_result_list_data[] = $item;
            }


            $data_json = [];   //返回前台json

            foreach ($result as $key => $value){
                $item = [];

                $student_type = 1;
                for($a = 0; $a < count($student_result_list_data); $a ++){
                    if($student_result_list_data[$a]['room_id'] == $value['room_id'] && $student_result_list_data[$a]['class_subject_id'] ==  $value['class_subject_id'] && $student_result_list_data[$a]['student_id'] ==  $value['student_id']){
                        $student_type = $student_result_list_data[$a]['student_type']; //学员类型
                    }
                }

                if ($student_type == 1) {
                    $item['student_name'] = $value['student_name'];//非补课学员
                } else if ($student_type == 2) {
                    $item['student_name'] = $value['student_name'] . '_【临加】';//临加学员
                } else if ($student_type == 3) {
                    $item['student_name'] = $value['student_name'] . '_【补课】';//补课学员
                }else if ($student_type == 4) {
                    $item['student_name'] = $value['student_name'] . '_【试听】';//试听学员
                }

                $content_area = $value['content_area'];  //内容
                $remarks_area = $value['remarks_area']; //备注
                $item['student_type'] = $student_type; //学生类型
                $item['class_manage_details_id'] = $value['class_manage_details_id'];
                $item['seat_number'] = $value['seat_number'];
                $item['class_subject_id'] = $value['class_subject_id'];
                $item['room_id'] = $value['room_id'];
                $item['student_id'] = $value['student_id'];
                $item['id'] = $value['id'];
                $item['surplus_class_elimination'] = $value['surplus_class_elimination']; //剩余课时
                $item['attendance_reasons'] = $value['attendance_reasons'];  //缺勤原因

                $item['phone'] = $student_data[$value['student_id'].''];
                $item['attendance'] = $value['attendance']; //考勤状态
                $item['charging'] = $value['charging']; //计费状态
                if($value['remarks'] == null){
                    $item['remarks'] = ''; //单个学生备注
                }else{
                    $item['remarks'] = $value['remarks']; //单个学生备注
                }
                $item['eliminated_nums'] = $value['eliminated_nums']; //计费次数
                if($value['eliminated_nums'] > 1){
                    $elimination_status = 1;
                }
                $item['surplus_class_elimination'] = $value['surplus_class_elimination']; //剩余课节数
                $item['surplus_class_elimination_give'] = $value['surplus_class_elimination_give']; //剩余赠送课节数
                $data_json[] = $item;

            }
            $json = json_encode($data_json);

        }

        return $this->fetch('',['elimination_status' => $elimination_status ,'jsondata' => $json, 'id' => $id,  'content_area' => $content_area, 'remarks_area' => $remarks_area,'campus_id'=>input('campus_id')]);
    }

	/**
	 * 添加学员列表
	 * modify by Small Y
	 *
	 * @return mixed
	 */
    public function add_students()
	{
        $info_id = input('info_id');
        $status = input('status'); //来源标志
        $campus_id = input('campus_id'); //来源标志
        if ($this->request->isAjax() && $this->request->isPost()) {
            $param = $this->request->param();
            $list = $this->studentModel->getAddStudentsLists($param);
            $this->success('success', '', $list, 0);
        }
        return $this->fetch('',[
        	'info_id' => $info_id,
        	'campus_id' => $campus_id,
			'status' => $status
		]);
    }

	/**
	 * 添加学员列表提交
	 * modify by Small Y
	 *
	 * @throws \think\exception\DbException
	 */
    public function submit_students()
	{
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            $info_id = $data['info_id']; //节课ID

            $infos_result = $this->gradesdetaillistModel->get(['id' => $info_id]);
            $student_id_arr = explode(',',$data['ids']);

            $student_add_arr =[]; //添加学生
            $student_add_Infos_arr =[]; //添加学生上课信息

                for ($i = 0; $i < count($student_id_arr); $i ++){
                    $students_result = $this->studentModel->get(['id' => $student_id_arr[$i]]);
                    $item =[];
                    $item['class_subject_id'] = $infos_result['class_manage_id']; //分班ID
                    $item['student_id'] = $student_id_arr[$i];  //学生ID
                    $item['campus_id'] = $students_result['campus_id']; //校区ID
                    $item['student_name'] = $students_result['name']; //学生姓名
                    $item['status'] = 1; //1:课程分配  2:班级分配  3:试听分配
                    if($students_result['is_audition'] == 0 && $students_result['sign_up'] != 1){   //试听
                        $item['student_type'] = 4; //试听学员
                        $item['contract_number'] = '---';

                    }
                    if($students_result['is_audition'] == 2 && $students_result['sign_up'] == 1){   //正式学员
                        $item['student_type'] =  2; //临时正式学员
                        $item['contract_number'] = '---';
                    }
                    $item['room_id'] = $infos_result['class_room_id']; //教室ID
                    $student_add_arr[] = $item;

                    $item_infos = [];
                    $item_infos['class_manage_details_id'] =  $info_id; //节课ID
                    $item_infos['class_subject_id'] = $infos_result['class_manage_id']; //分班ID
                    $item_infos['campus_id'] =  $infos_result['campus_id']; //校区ID
                    $item_infos['student_id'] = $student_id_arr[$i];//学生ID
                    $item_infos['student_name']  =  $students_result['name']; //学生姓名
                    $item_infos['room_name'] = $infos_result['class_room']; //教室名称
                    $item_infos['weekday'] =  $infos_result['weekday']; //星期
                    $item_infos['class_date'] = $infos_result['class_date']; //开课日期
                    $item_infos['course_id'] = $infos_result['course_id']; //课程ID


                    if($students_result['is_audition'] == 0 && $students_result['sign_up'] != 1){    //试听
                        $item_infos['contract_number'] = '---';
                    }

                    if($students_result['is_audition'] == 2 && $students_result['sign_up'] == 1){   //正式学员
                        $item_infos['contract_number'] = '---';
                    }


                    $item_infos['room_id'] = $infos_result['class_room_id'];//教室ID
                    $start = strtotime($infos_result['class_subject_start']);  //开始时间
                    $end = strtotime($infos_result['class_subject_end']);  //结束时间
                    $item_infos['duration'] = ceil(($end - $start)/60); //时长
                    $student_add_Infos_arr[] = $item_infos;

                }

            if(count($student_add_Infos_arr) > 0){
                if(!$this->studentsubjectinfosclassModel->saveAll($student_add_Infos_arr)){
                    $this->error('添加学员课节信息失败', null, -1, 1);
                }
            }

            if(count($student_add_arr) > 0){
                if(!$this->studentsubjectclassModel->saveAll($student_add_arr)){
                    $this->error('添加学员列表失败', null, -1, 1);
                }
            }


            $this->success('添加学员成功', '', '', 1);

        }

    }

    public function gradesstudentslists(){
        if ($this->request->isAjax() && $this->request->isPost()) {
            $param = $this->request->param();
            $param['class_subject_id'] = input('grades_id');
            $list = $this->studentsubjectclassModel->getGradesStudentsLists($param);
            $this->success('success', '', $list, 0);
        }
    }

	/**
	 * 查看作业
	 * User: wufeng
	 * Date: 2020/02/27
	 *
	 */
	public function look_work()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$course = (new WorkModel())->getWorkLists($param);
			$this->success('success', '', $course, 0);
		}

		return $this->fetch('',[
			'cmdl_id' => input('id')
		]);
	}

	/**
	 * 作业详情
	 * User: wufeng
	 * Date: 2020/04/03
	 *
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function look_work_detail()
	{
		$work = (new WorkModel())->get(input('id'));
		$work['audio_text'] = 'layui-icon-speaker';
		$work['video_text'] = 'layui-icon-video';
		$work['picture_text'] = 'layui-icon-picture';
		$picture = explode(',',$work['picture']);
		return $this->fetch('',[
			'work' => $work,
			'picture' => $picture
		]);
	}

	/**
	 * 查看解析
	 * User: wufeng
	 * Date: 2020/02/29
	 *
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function look_analysis()
	{
		$id = input('id');//作业id
		$analysis = (new AnswerAnalysisModel())->get(['work_id'=>$id]);
		$analysis['work_name'] = (new WorkModel())->get($id)['work_name'];
		$analysis['audio_text'] = 'layui-icon-speaker';
		$analysis['video_text'] = 'layui-icon-video';
		$analysis['picture_text'] = 'layui-icon-picture';
		$picture = explode(',',$analysis['picture']);
		return $this->fetch('',[
			'analysis' => $analysis,
			'picture' => $picture
		]);
	}

	/**
	 * 查看点评
	 * User: wufeng
	 * Date: 2020/02/29
	 *
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function look_comment()
	{
		if ($this->request->isAjax()) {
			$param = $this->request->param();
			$course = (new StudentSubjectInfosClassModel())->getStudentCommentLists($param);
			$this->success('success', '', $course, 0);
		}

		return $this->fetch('',[
			'cmdl_id' => input('id')
		]);
	}

	/**
	 * 点评详情
	 * User: wufeng
	 * Date: 2020/04/03
	 *
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
	public function look_comment_detail()
	{
		$where = [];
		$where['cmdl_id'] = input('cmdl_id');
		$where['student_id'] = input('student_id');
		$comment = (new CommentModel())->get($where);
		$comment['audio_text'] = 'layui-icon-speaker';
		$comment['video_text'] = 'layui-icon-video';
		$comment['picture_text'] = 'layui-icon-picture';
		$comment['publisher'] = (new UserModel())->get($comment['teacher_id'])['name'];
		$comment['student_name'] = (new StudentModel())->get($comment['student_id'])['student_name'];
		$picture = explode(',',$comment['picture']);
		return $this->fetch('',[
			'comment' => $comment,
			'picture' => $picture
		]);
	}

    /**
     * 实例化.
     * @return bool|void
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->arrangingModel = new ArrangingModel();
        $this->gradesdetaillistModel = new GradesDetailsListsModel();
        $this->studentsubjectinfosclassModel = new StudentSubjectInfosClassModel();
        $this->studentModel = new StudentModel();
        $this->contractModel = new ContractModel();
        $this->courseModel = new CourseModel();
        $this->gradesdetailModel = new GradesDetailsModel();
        $this->studentsubjectclassModel = new StudentSubjectClassModel();
        $this->courseContractModel =  new CourseContractModel();
        $this->campusModel = new CampusModel();
        $this->classroomModel = new ClassRoomModel();
        $this->courseLogModel = new CourseLogModel();
        $this->gradesContractModel = new GradesContractModel();
        $this->goodsContractModel = new GoodsContractModel();
    }
}
